{include file=header.html}
<div id="main" class="w-full">
    <div id="page-order" class="w">
        <div class="step step2 mt10 clearfix">
            <span class="fs14 strong f60">{$lang.ckgwc}</span>
            <span class="fs14 strong fff">{$lang.qrddxx}</span>
            <span class="fs14 strong">{$lang.fk}</span>
            <span class="fs14 strong">{$lang.qrsh}</span>
            <span class="fs14 strong">{$lang.pj}</span>
        </div>
        <div class="order-form">
            <form method="post" id="order_form">
<!--order.amount.html script_start-->
<script type="text/javascript">
    $(function () {
        $('.use_integral_button').click(function () {
            $(this).parent('p').next().toggle();
        });
    });
</script>
<script type="text/javascript">
    $(function () {
        $('.use_coupon').click(function () {
            $(this).parent('p').next().toggle();
            //$(this).hide();
            var store_id=$(this).attr('data-store_id');
            if ($(this).val() == '{$lang.use_coupon}') {
                $(this).val('{$lang.close_coupon}');
                $("select[name='coupon_sn["+store_id+"]']")[0].selectedIndex = 0;
            } else {
                $(this).val('{$lang.use_coupon}')
            }
        });
    });
</script>
<script type="text/javascript">
    $(function () {
        $('#use_pd_amount').click(function () {
            $(this).parent('p').next().toggle();
        });
    });
</script>
<!--order.amount.html script_end-->


<!--order.shipping.html script_start-->
<script type="text/javascript" src="{lib file=mlselection.js}" charset="utf-8"></script>
<script type="text/javascript" src="{lib file=jquery.plugins/jquery.validate.js}" charset="utf-8"></script>
<script type="text/javascript" src="{lib file=dialog/dialog.js}" id="dialog_js" charset="utf-8"></script>
<script type="text/javascript" src="{lib file=jquery.ui/jquery.ui.js}" id="dialog_js" charset="utf-8"></script>
<script type="text/javascript">
                var addresses = {$delivery_data.addresses};
                var all_regions={$all_regions};
                var shippings = new Array();
                var goods_amount = new Array();
                var goods_quantity = new Array();

                var total_integral_max_exchange = new Array();
                var order_amount=new Array(); //�¼�
                var store_integral=new Array();
                var is_free_fee=new Array();

                var addresses_array;
                addresses_array=eval(addresses);

                var member_integral={$member_info.integral|default:0};//新加
                var integral_enabled={$integral_enabled|default:0};
                $(function(){
                    regionInit("region");
                    $('#order_form').validate({
                        invalidHandler:function(e, validator){
                         var err_count = validator.numberOfInvalids();
                         var msg_tpl = '{$lang.order_have_error}';
                         var d = DialogManager.create('show_error');
                         d.setWidth(400);
                         d.setTitle(lang.error);
                         d.setContents('message', {type:'warning', text:$.format(msg_tpl, err_count)});
                         d.show('center');
                        },
                        errorPlacement: function(error, element){
                            var _message_box = $(element).parent().find('.field_message');
                            _message_box.find('.field_notice').hide();
                            _message_box.append(error);
                        },
                        success       : function(label){
                            label.addClass('validate_right').text('OK!');
                        },
                        rules : {
                            consignee : {
                                required : true
                            },
                            region_id : {
                                required : true,
                                min   : 1
                            },
                            address   : {
                                required : true
                            },
                            phone_mob : {
                                required : check_phone,
                                minlength:6,
                                digits : true
                            }
                        },
                        messages : {
                            consignee : {
                                required : '{$lang.consignee_required}'
                            },
                            region_id : {
                                required : '{$lang.region_required}',
                                min  : '{$lang.region_required}'
                            },
                            address   : {
                                required : '{$lang.address_required}'
                            },
                            phone_mob : {
                                required : '{$lang.phone_required}',
                                minlength: '{$lang.phone_mob_error}',
                                digits : '{$lang.phone_mob_error}'
                            }
                        }
                    });

                    $('ul[shipping_id]').each(function(){
                        var store_id=$(this).attr('store_id');
                        var _shipping_fee = get_shipping_fee($(this).attr('shipping_id'),store_id);
                        $(this).find('[ectype="shipping_fee"]').html(price_format(_shipping_fee));
                    }).click(function(){
                        $(this).parent().find(".shipping_info").hide();
                        $(this).find(".shipping_info").show();

                        var store_id=$(this).attr('store_id');
                        $(this).find('input[name="shipping_id_'+store_id+'"]').attr('checked', true);

                        if(integral_enabled && total_integral_max_exchange[store_id]>0 && member_integral>0){
                            set_order_amount_total(store_id);
                        }else{
                            set_order_amount(store_id);
                        }

                    });

                });
                function set_order_amount(store_id){
                    var shipping_id = $('input[name="shipping_id_'+store_id+'"]:checked').val();
                    var _shipping_fee=get_shipping_fee(shipping_id,store_id);
                    var _amount  = ForDight((goods_amount[store_id] + _shipping_fee),2);
                    order_amount[store_id]=_amount;
                    $('#order_amount_'+store_id).html(price_format(_amount));
                    var total_orders_amount=0;
                    for(var key in order_amount){
                        total_orders_amount=ForDight((total_orders_amount+order_amount[key]),2);
                    }
                    $('#total_orders_amount').html(price_format(total_orders_amount));
                }
                function set_order_amount_total(store_id){
                    var shipping_id = $('input[name="shipping_id_'+store_id+'"]:checked').val();
                    var _shipping_fee=get_shipping_fee(shipping_id,store_id);
					var coupon_sn_money = 0;
                    var  coupon_sn =document.getElementById("coupon_sn_"+store_id+"");
                    var index=coupon_sn.selectedIndex ;
                    var coupon_sn_text=  coupon_sn.options[index].text;
                    var coupon_sn_text = /[^\(\)]+(?=\))/g.exec(coupon_sn_text);
                    var coupon_sn_text = String(coupon_sn_text);
                    var coupon_sn_text =parseFloat(coupon_sn_text.replace("¥",""));
                    if(coupon_sn_text){
                         coupon_sn_money = coupon_sn_text;
                    }
                    var min_integral;
                    //当前设置积分数
                    var use_integral  = parseInt($('#use_integral_'+store_id).val());
                    //当设置积分为负数时
                    if(use_integral<0 || isNaN(use_integral)){
                        $('#use_integral_'+store_id).val('');
                        use_integral=0;
                    }
                    //integral_start
                    //找到最小的
                    member_integral={$member_info.integral|default:0};
                    for(var key in store_integral){
                        if(key != store_id){
                            member_integral=member_integral-store_integral[key];
                        }
                    }
                    min_integral=member_integral;
                    if(total_integral_max_exchange[store_id]<min_integral){
                        min_integral=total_integral_max_exchange[store_id];

                    }
                    if({$integral_seller|default:0}){
                        var max_goods_integray=parseInt(goods_amount[store_id]/{$integral_seller|default:0});
                        if(max_goods_integray<min_integral){
                            min_integral=max_goods_integray;

                        }
                    }
                    if(use_integral>min_integral){
                        use_integral=min_integral;
                    }
                    //$("body").prepend(use_integral+'B<br>');
                    //integral_end

                        store_integral[store_id]=use_integral;
                        if(use_integral){
                            $('#use_integral_'+store_id).val(use_integral);
                        }else{
                            $('#use_integral_'+store_id).val('');
                        }


                        //积分兑换比例 得出具体抵扣数值
                        var _integral_fee = use_integral*{$integral_seller|default:0};
                        var _amount  = goods_amount[store_id] + _shipping_fee - _integral_fee - coupon_sn_money;
                        order_amount[store_id]=_amount;
                        $('#order_amount_'+store_id).html(price_format(ForDight(_amount,2)));

                    var total_orders_amount=0;
                    for(var key in order_amount){
                        total_orders_amount=ForDight((total_orders_amount+order_amount[key]),2);
                    }
                    $('#total_orders_amount').html(price_format(total_orders_amount));
                }
//四舍五入函数
function   ForDight(Dight,How)
{
            Dight   =   Math.round   (Dight*Math.pow(10,How))/Math.pow(10,How);
            return   Dight;
}

                function get_shipping_fee(shipping_id,store_id){
                    if(!shipping_id){
                        return 0;
                    }
                    var shipping_data = shippings[store_id][shipping_id];
                    var first_price   = Number(shipping_data['first_price']);
                    var step_price   = Number(shipping_data['step_price']);

                    var cod_regions_price=shipping_data['cod_regions_price'];


                  //获取地区价格
                  var ajex_region_id;

                  if(parseInt($("input[name='address_options']:checked").val())>0){
                  ajex_region_id=addresses_array[$("input[name='address_options']:checked").val()]['region_id'];
                  }else{
                  if($("#region_id").val()){
                      ajex_region_id=$("#region_id").val();
                  }
                  }

                  if(ajex_region_id){
                  	var suitble_region=0;

                  for(var r_p in cod_regions_price){

                  	var region_price_array;
                    if(ajex_region_id==r_p){


                        region_price_array=cod_regions_price[r_p].split(",");

                        first_price = parseFloat(region_price_array[1]);
                        step_price   = parseFloat(region_price_array[2]);

                            break;

                    }
                    $.ajaxSettings.async = false;
                   //获取地区的子孙
                   if(typeof(all_regions[ajex_region_id])!='undefined'){
                       if($.inArray(r_p,all_regions[ajex_region_id])>-1){
                                  region_price_array=cod_regions_price[r_p].split(",");

                                  if((suitble_region==0) || (typeof(all_regions[r_p])!='undefined' && (all_regions[r_p].length >suitble_region)))
                                  {
                                      suitble_region=all_regions[r_p].length;
                        first_price = parseFloat(region_price_array[1]);
                        step_price   = parseFloat(region_price_array[2]);

                                  }
                       }
                   }





                  }

                  }
                  var _shipping_fee=first_price + (goods_quantity[store_id] - 1) * step_price;
                  if(shipping_id){
                		$('ul[shipping_id='+shipping_id+']').find('[ectype="shipping_fee"]').html(price_format(_shipping_fee));
                	}
                	return _shipping_fee;
                }
                function check_phone(){
                    return ($('#phone_mob').val() == '');
                }
                function hide_error(){
                    $('#region').find('.error').hide();
                }
                </script>
<script type="text/javascript">
                //<![CDATA[
                $(function(){
                    //$("input[name='address_options']").click(set_address);
                    $('.address_item').click(function(){
                        $(this).find("input[name='address_options']").attr('checked', true);
                        $('.address_item').removeClass('selected_address');
                        $(this).addClass('selected_address');
                        set_address();
                    });
                    //init
                    set_address();


                    $("input[name='address_options']").change(function(){
                        <!--{foreach from=$goods_info_array item=goods_info key=key }-->
                            <!--{if $integral_enabled && $goods_info.total_integral_max_exchange gt 0 && $member_info.integral gt 0}-->
                            set_order_amount_total({$key});
                            <!--{else}-->
                            set_order_amount({$key});
                            <!--{/if}-->
                        <!--{/foreach}-->
                                });
                    $(".address_item").click(function(){
                        <!--{foreach from=$goods_info_array item=goods_info key=key }-->
                            <!--{if $integral_enabled && $goods_info.total_integral_max_exchange gt 0 && $member_info.integral gt 0}-->
                            set_order_amount_total({$key});
                            <!--{else}-->
                            set_order_amount({$key});
                            <!--{/if}-->
                        <!--{/foreach}-->
                                });
                    $("#region").click(function(){
                        <!--{foreach from=$goods_info_array item=goods_info key=key }-->
                            <!--{if $integral_enabled && $goods_info.total_integral_max_exchange gt 0 && $member_info.integral gt 0}-->
                            set_order_amount_total({$key});
                            <!--{else}-->
                            set_order_amount({$key});
                            <!--{/if}-->
                        <!--{/foreach}-->
                                });
                    $("input[name='address_options']:checked").change();


                });
                function set_address(){
                    var addr_id = $("input[name='address_options']:checked").val();
                    if(addr_id == 0)
                    {
                        $('#consignee').val("");
                        $('#region_name').val("");
                        $('#region_id').val("");
                        $('#region select').show();
                        $("#edit_region_button").hide();
                        $('#region_name_span').hide();
                        $('#address').val("");
                        $('#zipcode').val("");
                        $('#phone_mob').val("");
                        //lichen 2019/4/12 start
                        $('#user_no').val("");
                        //lichen 2019/4/12 end
                        $('#address_form').show();
                    }
                    else
                    {
                        $('#address_form').hide();
                        fill_address_form(addr_id);
                    }
                }
                function fill_address_form(addr_id){
                    var addr_data = addresses[addr_id];
                    for(k in addr_data){
                        switch(k){
                            case 'consignee':
                            case 'address':
                            case 'zipcode':
                            case 'email':
                            case 'phone_mob':
                            // lichen 2019/4/12 start
                            case 'user_no':
                            // lichen 2019/4/12 end
                            case 'region_id':
                                $("input[name='" + k + "']").val(addr_data[k]);
                            break;
                            case 'region_name':
                                $("input[name='" + k + "']").val(addr_data[k]);
                                $('#region select').hide();
                                $('#region_name_span').text(addr_data[k]).show();
                                $("#edit_region_button").show();
                            break;
                        }
                    }
                }
                //]]>
              </script>
<!--order.shipping.html script_end-->
<div id="select-address" class="w mt20">
   <div class="title w mb10">
      <b class="fs14">{$lang.consignee_address}</b>
	  <a href="{url app=my_address}" target="_blank">[{$lang.manage_address}]</a>
   </div>
   <!--{if $delivery_data.my_address}-->
   <div class="oldaddress w">
      <!--{foreach from=$delivery_data.my_address item=address name=address_select}-->
      <dl class="f66 clearfix address_item{if $address.is_default} selected_address{/if}">
         <dt class="float-left"><input id="address_{$address.addr_id}" type="radio"{if $address.is_default} checked="true"{/if} name="address_options" value="{$address.addr_id}" /></dt>
	<dd class="float-left">
         	{$address.consignee|escape}&nbsp;&nbsp;&nbsp;&nbsp;{$address.region_name|escape}&nbsp;&nbsp;&nbsp;&nbsp;{$address.address|escape}&nbsp;&nbsp;&nbsp;&nbsp;
         	<!--{if $address.phone_mob}-->{$address.phone_mob}<!--{/if}-->
            <!--lichen 2019/4/12 start-->
            {if $address.user_no}{$address.user_no}{/if}
            <!--lichen 2019/4/12 end-->
         </dd>
      </dl>
      <!--{/foreach}-->
   </div>
   <!--{/if}-->
   <div class="f66 w newaddress address_item">
    <input class="mb5" type="radio" name="address_options" id="use_new_address" value="0" {if !$delivery_data.my_address} checked="true" {/if}/> {$lang.use_new_address}
   </div>
   <div class="newaddressform w">
      <ul class="fill_in_content mt10" id="address_form">
         <li>
            {$lang.consignee}{$lang.colon}<input type="text" name="consignee" id="consignee" class="width5"  />
            <span class="field_message explain"><span class="field_notice">{$lang.consignee_desc}</span></span>
         </li>
         <li class="clearfix">
		     <div class="float-left">{$lang.region}{$lang.colon}</div>
             <div id="region" class="float-left">
                <span style="display:none;" id="region_name_span"></span>
                <input id="edit_region_button" type="button" class="edit_region" value="{$lang.edit}" style="display:none;" />
                <select onchange="hide_error();">
                   <option value="0">{$lang.select_pls}</option>
                   {html_options options=$delivery_data.regions}
                </select>
                <input type="hidden" class="mls_id" name="region_id" id="region_id"/>
			    <input type="hidden" name="region_name" class="mls_names" id="region_name"/>
                <span class="field_message explain"><span class="field_notice">{$lang.region_desc}</span></span>
             </div>
          </li>
          <li>
             {$lang.address}{$lang.colon}<input type="text" name="address" id="address" class="width7" />
             <span class="field_message explain"><span class="field_notice">{$lang.address_desc}</span></span>
          </li>
          <li>
             {$lang.zipcode}{$lang.colon}<input type="text" name="zipcode" id="zipcode" class="width5" />
             <span class="field_message explain"><span class="field_notice">{$lang.zipcode_desc}</span></span>
          </li>
          <li>
             {$lang.phone_mob}{$lang.colon}<input type="text" id="phone_mob" name="phone_mob"  class="width5"/>
             <span class="field_message explain"><span class="field_notice">{$lang.phone_mob_desc}</span></span>
          </li>
          <!--lichen 2019/4/11 start-->
          <li>
             {$lang.user_no}{$lang.colon}<input type="text" id="user_no" name="user_no"  class="width5"/>
             <span class="field_message explain"><span class="field_notice">{$lang.user_no_desc}</span></span>
          </li>
          <!--lichen 2019/4/11 end-->
          <li><input  style="border:0;" type="checkbox" value="1" id="save_address" name="save_address"><span class="explain f66">{$lang.save_address}({$lang.save_address_desc})</span></li>
       </ul>
   </div>
</div>
<!--{foreach from=$goods_info_array item=goods_info key=key }-->
                {include file=order.shipping.html}
                {include file=order.goods.html}
                {include file=order.postscript.html}
                {include file=order.amount.html}
<!--{/foreach}-->
<div class="w price-promotion mt10 mb10 f60">
    <p class="mb10 mt10">
        <input id="use_pd_amount" type="button" class="btn-allow-coupon center" value="{$lang.use_pd_amount}" />
    </p>
    <p class="allow-coupon border padding10 mb10 hidden clearfix">
        <label>
            <input type="checkbox" name="pd_amount" value="{$member_info.money}" style="margin-top:9px">
            <span class="note">{$lang.use_pd_amount}（{$lang.account_cur_money}：{$member_info.money}{$lang.account_cur_money}）</span>
        </label>
    </p>
</div>
                <div class="make_sure">
                    <a href="{url app=cart}" class="back">{$lang.backto_cart}</a>
                    <span style="margin-left:20px;">{$lang.need_pay}：<strong id="total_orders_amount"></strong></span>
                <a href="javascript:void($('#order_form').submit());" class="btn-step fff center strong fs14 ml20">{$lang.shopping_step_three}</a>
                </div>
                <div class="w price-notice mt10 mb10 pt10 f60 pb10">{$lang.price_notice}</div>
            </form>
        </div>
    </div>
</div>
{include file=server.html}
{include file=footer.html}